<template>
  <div class="action-select">
    <!-- <van-cell
      is-link
      :title="title"
      :value="showValue"
      @click="showAction = true"
    /> -->
    <van-field
      v-model="showValue"
      is-link
      readonly
      :disabled="disabled"
      :label="title"
      placeholder="请选择"
      input-align="right"
      @click="disabled ? null : showAction = true"
    />
    <van-action-sheet v-model="showAction">
      <div class="content">
        <div
          class="action-item"
          v-for="item in list"
          :key="item[value]"
          @click="selectItem(item)"
        >
          {{ item[label] }}
        </div>
      </div>
    </van-action-sheet>
  </div>
</template>

<script>
export default {
  name: "ActionSelect",
  props: {
    select: {
      type: [String, Number],
      default: "",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    list: {
      type: Array,
      default: () => [],
    },
    label: {
      type: String,
      default: "label",
    },
    value: {
      type: String,
      default: "value",
    },
    title: {
      type: String,
      default: "",
    },
  },
  model: {
    prop: "select",
    event: "change",
  },
  data() {
    return {
      showAction: false,
    };
  },
  computed: {
    showValue() {
      if (this.select === null || this.select === undefined || this.select === '') return ""
      const item = this.list.find((item) => item[this.value] === this.select);
      return item ? item[this.label] : this.select;
    },
  },
  methods: {
    selectItem(item) {
      this.$emit("change", item[this.value]);
      this.showAction = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.action-select {
  .action-item {
    padding: 15px;
    text-align: center;
    font-size: 16px;
    &:active {
      background-color: #eee;
    }
  }
}
</style>
